module.exports = function(app) {
  
	const New = app.models.New;

	this.indexView = function(req, res) {
   
		const news = New.query;
		let limit = 12;
		let offset = 0;

		news.findAndCountAll().then(( data ) => {	
			let page = req.params.page;
	    let pages = Math.ceil(data.count / limit);
			offset = limit * (page - 1);
			news.findAll({ 
					order: [ [ 'createdAt', 'DESC' ]],
					limit: 12
				}).then(news => {
					res.render("index", {
						layout: 'layouts/layout',
						news: news,
						pagesNumber: pages,
						currentPage: 1
					});
				});
		})

	}

	this.filterByPagination = ( req, res ) => {
		const news = New.query;
		const limit = 12;

		news.findAndCountAll().then(( data ) => {	
			let page = req.params.page;
	    let pages = Math.ceil(data.count / limit);
			offset = limit * (page - 1);
			news.findAll({ 
					order: [ [ 'createdAt', 'DESC' ]],
					limit: 12,
					offset: offset,
      		$sort: { id: 1 }
				}).then(news => {
					res.render("index", {
						layout: 'layouts/layout',
						news: news,
						pagesNumber: pages,
						currentPage: page
					});
				});
		})
	}

  this.postView = ( req, res ) => {

    const news = New.query;
    news.findOne({ where: { id: req.params.post_id } }).then(post => {
      res.render("single", {
        layout: 'layouts/layout',
        post: post,
      });
    })

  }

	return this;

}